﻿using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace FCNS.Windows
{
    /// <summary>
    /// 鼠标模拟事件
    /// </summary>
    public static class MouseSimulator
    {
        /// <summary>
        /// 获取或设置当前鼠标的坐标
        /// </summary>
        public static Point Position
        {
            get
            {
                return new Point(Cursor.Position.X, Cursor.Position.Y);
            }
            set
            {
                Cursor.Position = value;
            }
        }
        /// <summary>
        /// 获取或设置当前鼠标的 X 坐标
        /// </summary>
        public static int X
        {
            get
            {
                return Cursor.Position.X;
            }
            set
            {
                Cursor.Position = new Point(value, Y);
            }
        }
        /// <summary>
        /// 获取或设置当前鼠标的 Y 坐标
        /// </summary>
        public static int Y
        {
            get
            {
                return Cursor.Position.Y;
            }
            set
            {
                Cursor.Position = new Point(X, value);
            }
        } 

        static void MouseDown(WinApi.MouseButton button)
        {
           WinApi.mouse_event(((int)button), 0, 0, 0, 0);
        }
        /// <summary>
        /// 按下鼠标按键
        /// </summary>
        /// <param name="button">鼠标按键</param>
        public static void MouseDown(MouseButtons button)
        {
            switch (button)
            {
                case MouseButtons.Left:
                    MouseDown(WinApi.MouseButton.Left);
                    break;
                case MouseButtons.Middle:
                    MouseDown(WinApi.MouseButton.Middle);
                    break;
                case MouseButtons.Right:
                    MouseDown(WinApi.MouseButton.Right);
                    break;
            }
        }

        static void MouseUp(WinApi.MouseButton button)
        {
            WinApi.mouse_event(((int)button) * 2, 0, 0, 0, 0);
        }
        /// <summary>
        /// 释放鼠标按键
        /// </summary>
        /// <param name="button">鼠标按键</param>
        public static void MouseUp(MouseButtons button)
        {
            switch (button)
            {
                case MouseButtons.Left:
                    MouseUp(WinApi.MouseButton.Left);
                    break;
                case MouseButtons.Middle:
                    MouseUp(WinApi.MouseButton.Middle);
                    break;
                case MouseButtons.Right:
                    MouseUp(WinApi.MouseButton.Right);
                    break;
            }
        }

        static void Click(WinApi.MouseButton button)
        {
            MouseDown(button);
            MouseUp(button);
        }
        /// <summary>
        /// 按下并释放鼠标按键
        /// </summary>
        /// <param name="button">鼠标按键</param>
        public static void Click(MouseButtons button)
        {
            switch (button)
            {
                case MouseButtons.Left:
                    Click(WinApi.MouseButton.Left);
                    break;
                case MouseButtons.Middle:
                    Click(WinApi.MouseButton.Middle);
                    break;
                case MouseButtons.Right:
                    Click(WinApi.MouseButton.Right);
                    break;
            }
        }

        public static void DoubleClick(WinApi.MouseButton button)
        {
            Click(button);
            Click(button);
        }
        /// <summary>
        /// 按下并释放鼠标按键2次
        /// </summary>
        /// <param name="button">鼠标按键</param>
        public static void DoubleClick(MouseButtons button)
        {
            switch (button)
            {
                case MouseButtons.Left:
                    DoubleClick(WinApi.MouseButton.Left);
                    break;
                case MouseButtons.Middle:
                    DoubleClick(WinApi.MouseButton.Middle);
                    break;
                case MouseButtons.Right:
                    DoubleClick(WinApi.MouseButton.Right);
                    break;
            }
        }

        /// <summary>
        /// 滚动鼠标滑轮
        /// </summary>
        /// <param name="delta"></param>
        public static void MouseWheel(int delta)
        {
            WinApi.mouse_event(WinApi.MOUSEEVENTF_WHEEL, 0, 0, delta, 0);
        }
        /// <summary>
        /// 显示鼠标光标
        /// </summary>
        public static void Show()
        {
            WinApi.ShowCursor(true);
        }
        /// <summary>
        /// 隐藏鼠标光标
        /// </summary>
        public static void Hide()
        {
            WinApi.ShowCursor(false);
        } 
    }

}
